const {
    Mysql
} = require('../../tools');

module.exports = async ctx => {

    let params = ctx.request.body;
    let {
        type
    } = params;
    // 如果是电视剧
    if (type == 1) {
        let {
            vid,
            title,
            coverImg,
            isFree,
            isNew,
            isVip,
            price,
            type,
            sort,
            qisim
        } = params;
        let sql = `UPDATE video SET title='${title}',coverImg='${coverImg}',type='${type}',sort='${sort}',isFree=${Number(isFree)},isNew=${Number(isNew)},isVip=${Number(isVip)},price=${price?parseFloat(price):null} WHERE vid =${vid} `;
        let sql2 = `REPLACE INTO tv (vid,title,coverImg,qisim) VALUES(${vid},'${title}','${coverImg}','${qisim}')`;
        try {
            let res = await Mysql(sql);
            let res2 = await Mysql(sql2)
            if (res && res2) {
                ctx.state.data = {
                    code: '001',
                    msg: 'مۇۋەپپىقىيەتلىك يېڭىلاندى'
                }
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                msg: 'مەغلۇپ بولدى'
            }
        }

    } else {
        // 如果是电影
        let {
            vid,
            title,
            videoUrl,
            coverImg,
            sort,
            isFree,
            isNew,
            isVip,
            price,
            type,
        } = params;
        let sql = `UPDATE video SET title='${title}',videoUrl='${videoUrl}',coverImg='${coverImg}',type=${Number(type)},sort='${sort}',isFree=${Number(isFree)},isNew=${Number(isNew)},isVip=${Number(isVip)},price=${price?parseFloat(price):null} WHERE vid =${vid} `;
        try {
            let res = await Mysql(sql);
            if (res) {
                ctx.state.data = {
                    code: '001',
                    msg: 'مۇۋەپپىقىيەتلىك يېڭىلاندى'
                }
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                msg: 'مەغلۇپ بولدى'
            }
        }
    }

};